<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');
 
// Funzione per la preview del testo del messaggio.
function preview($txt)
{ 
	$pre = $txt;
	$pre = str_replace("<br />", " ", $pre);
	$pre = str_replace("<hr>", " ", $pre);
	$pre = strip_tags($pre);
	$pre = html_entity_decode($pre);
	$preold = $pre;
	if (strlen($pre) > 0) {
		$pre = substr($pre, 0, 250);
		if ($pre != $preold) {$pre .= "...";};
	};
	return(htmlentities($pre));
}  

$topic_id = ( ! empty($_GET['topic'])) ? trim(mysql_real_escape_string($_GET['topic'])) : 0;
$board_id = ( ! empty($_GET['board'])) ? trim(mysql_real_escape_string($_GET['board'])) : 0;

$topic_new		= '';
$topics_list	= '';
$topic_mod		= '';
 
if ($topic_id == 0)
{
	$back = '<a href="boards.php" class="btn btn-default" target="_self"><b class="glyphicon glyphicon-chevron-left"></b> Indietro</a><hr />';
	
	$topic_new = '<a href="board-write.php?act=1&board=' . $board_id . '" class="btn btn-primary" target="_self">Nuovo Topic</a>';
	
	//Recupera l'elenco delle  bacheche.
	$query = "SELECT * FROM topics WHERE topic_board='$board_id' AND topic_parent = 0 AND topic_title != '' ORDER BY topic_highlight DESC, topic_lastreply DESC";
	if($r1 = $db -> query("SELECT * FROM topics WHERE topic_board='$board_id' AND topic_parent = 0 AND topic_title != '' ORDER BY topic_highlight DESC, topic_lastreply DESC"))
	{
		while($rd1 = $r1 -> fetch_assoc())
		{
			// Variabili
			$author_id			= $rd1['topic_author'];
			$topic_highlight 	= '';
			$topic_lock 		= '';
			
			// Recupera le informazioni dell'autore.
			if($r2 = $db -> query("SELECT user_name, user_surname FROM users WHERE user_id = '$author_id'")) 
			{
				$rd2 = $r2 -> fetch_assoc();
				
				$topic_author = $rd2['user_name'];
				if ($rd1['topic_highlight'] == 1)
				{
					// Se il topic � in evidenza, aggiunge un'immagine prima del titolo.
					$topic_highlight = '<img src="style/images/icon-star.gif" alt="Highlight" title="Thread in evidenza" border="0"/> ';
				}
				if ($rd1['topic_lock'] == 1)
				{
					// Se il topic � in evidenza, aggiunge un'immagine prima del titolo.
					$topic_lock = '<img src="style/images/icon-lock.png" alt="Locked" title="Thread chiuso" border="0"/> ';
				}
				
				$topics_list .= '<div class="list-group">';
				$topics_list .= '<a href="topics.php?board=' . $rd1['topic_board'] . '&topic=' . $rd1['topic_id'] . '" class="list-group-item" target="_self">';
				$topics_list .= $topic_lock . $topic_highlight . '<h4>' . $rd1['topic_title'];
				$topics_list .= ' <small>Aperto da: <b>' . $topic_author . '</b> | il: <b>' . date('d/m/Y H:i', strtotime($rd1['topic_date'])) . '</b></small></h4><p>' . preview($rd1['topic_content']) . '</p>';
				$topics_list .= '<p><b>Visto: ' . $rd1['topic_views'];
				$topics_list .= ' | Risposto: ' . $rd1['topic_replies'];
				$topics_list .= ' | Risposta: ' . $rd1['topic_lastreply'] . '</b></p></a>';
			}
			
		}
		$r1 -> close();
		$r2 -> close();
	}
} else {
	// Variabili
	$thread_tools	= ''; // Tools per il thread
	$post_tools		= ''; // Tools per i messaggi di risposta

	$back = '<a href="topics.php?board=' . $board_id . '" class="btn btn-default" target="_self"><b class="glyphicon glyphicon-chevron-left"></b> Indietro</a><hr />';

	// Seleziona il topic principale
	if($r1 = $db -> query("SELECT topic_id, topic_author, topic_content, topic_date, topic_title, topic_highlight, topic_lock FROM topics WHERE topic_id = $topic_id"))
	{
		while($rd1 = $r1 -> fetch_assoc())
		{
			
			// Se l'utente � master, mostra le tools
			if ($_SESSION['master'] == 1)
			{
				if ($rd1['topic_highlight'] == 1)
				{
					// Il topic � gi� evidenziato, mostra controlli per togliere l'evidenziazione.
					$tools_highlight = '<input type="checkbox" name="highlight" id="highlight" value="2" /><label for="highlight"><img src="style/images/icon-nostar.gif" title="Rimuovi Evidenziazione" border="0"/></label>';
				} else {
					$tools_highlight = '<input type="checkbox" name="highlight" id="highlight" value="1" /><label for="highlight"><img src="style/images/icon-star.gif" alt="Highlight" title="Evidenzia Topic" border="0"/></label>';
				}
				
				if ($rd1['topic_lock'] == 1)
				{ // Il topic � bloccato, mostra controlli per sbloccare.
					$tools_lock = '<input type="checkbox" name="lock" value="2" id="lock" /><label for="lock"><img src="style/images/icon-key.png" alt="Locked" title="Apri Thread" border="0"/></label>';			
				} else {
					$tools_lock = '<input type="checkbox" name="lock" value="1" id="lock" /><label for="lock"><img src="style/images/icon-lock.png" alt="Locked" title="Chiudi Thread" border="0"/></label>';			
				}
				
				$thread_tools = '<div class="alert alert-default"><form action="board-write.php?act=4" method="post">';
				$thread_tools .= '<input type="hidden" name="parent_id" value="' . $rd1['topic_id'] . '" />' . $tools_highlight;
				$thread_tools .= '<input type="checkbox" value="1" name="delete" id="delete" />';
				$thread_tools .= '<label for="delete"><img src="style/images/icon-delete.png" alt="Delete" title="Cancella Topic" border="0"/></label>';
				$thread_tools .= $tools_lock . ' <input type="submit" class="btn btn-default" value="Conferma" /></form></div>';
			} else {
				$thread_tools = 'nessuno';
			}
			
			// Variabili
			$author_id = $rd1['topic_author'];
			
			// Recupera le informazioni dell'autore.
			if($r2 = $db -> query("SELECT user_name, user_surname FROM users WHERE user_id = '$author_id'")) 
			{
				$rd2 = $r2 -> fetch_assoc();
				
				$topic_author = $rd2['user_name'];
				// Se l'utente � l'autore del messaggio, mostra i controlli di modifica.
				if ($_SESSION['uid'] == $author_id)
				{
					$topic_mod .= '<a href="board-write.php?act=3&board=' . $board_id . '&topic=' . $rd1['topic_id'].'" class="btn btn-link" target="_self">Modifica</a>';
				}
				// Stampa il primo messaggio.
				$topics_list .= '<h4>' . $rd1['topic_title'];
				$topics_list .= ' <small><b>' . $topic_author . ' | ' . date('d/m/Y H:i', strtotime($rd1['topic_date'])) . '</b></small></h4>';
				$topics_list .= $thread_tools;
				$topics_list .= '<p>' . bbtext(htmlentities($rd1['topic_content'])) . '</p><p>' . $topic_mod . '</p>';
			
				// Crea il tasto di risposta.
				if ($rd1['topic_lock'] == 0)
				{
					$topic_new = '<a href="board-write.php?act=2&board=' . $board_id . '&parent=' . $rd1['topic_id'] . '" class="btn btn-success" target="_self">Rispondi</a>';
				} else {
					$topic_new = '<button type="input" class="btn btn-default" disabled="disabled">Thread Chiuso</button>';
				}
				
				// Aggiunge +1 alle visualizzazioni.
				$db -> query("UPDATE topics SET topic_views = topic_views + 1 WHERE topic_id = '$topic_id'");
			}
			
		} // fine ciclo while
		
		// Seleziona i topics di risposta.
		if($r3 = $db -> query("SELECT topic_id, topic_author, topic_content, topic_date FROM topics WHERE topic_parent = '$topic_id'"))
		{
			while($rd3 = $r3 -> fetch_assoc())
			{
				
				// Se l'utente � master, mostra le tools
				if ($_SESSION['master'] == 1)
				{
					$post_tools = '<div class="alert alert-default"><form action="board-write.php?act=4" method="post">';
					$post_tools .= '<input type="hidden" name="topic_id" value="' . $rd3['topic_id'] . '" />';
					$post_tools .= '</label> <input type="submit" class="btn btn-danger" value="Cancella" /></form></div>';
				} else {
					$post_tools = 'nessuno';
				}
				
				// Variabili
				$author_id = $rd3['topic_author'];
				// Recupera le informazioni dell'autore.
				if($r4 = $db -> query("SELECT user_name, user_surname FROM users WHERE user_id = '$author_id'")) 
				{
					$rd4 = $r4 -> fetch_assoc();
					
					$reply_author = $rd4['user_name'];
					// Se l'utente � l'autore del messaggio, mostra i controlli di modifica.
					if ($_SESSION['uid'] == $author_id)
					{
						$topic_mod = '<a href="board-write.php?act=3&board=' . $board_id . '&topic=' . $rd3['topic_id'] . '" class="btn btn-link" target="_self">Modifica</a>';
					}
					
					// Stampa i messaggi di risposta.
					$topics_list .= '<h4>' . $rd3['topic_date'] . ' | <b>' . $reply_author . '</b></h4>';
					$topics_list .= $post_tools;
					$topics_list .= '<p>' . bbtext(htmlentities($rd3['topic_content'])) . '</p><p>' . $topic_mod . '</p>';
				}
				
			}
		}
	}
}

// Lista delle tag speciali
$tags = array('temp_title' => $g_name, 'temp_topicslist' => $topics_list, 'temp_newtopic' => $topic_new, 'temp_back' => $back);

// Carica il template e formatta le tag speciali
$temp = new template('style/topics.template.html');
$temp -> replace($tags);

// Visualizza l'output
echo $temp -> show();

// Chiude la connessione al database
$db -> close();

?>